<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 订单人数更改日志模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Ordernumberlog_model extends CI_Model {

    private $sid;
    private $table;
    private $period_price_array;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->table = $this->db->dbprefix("order_number_log");
        $this->period_price_array = array(
            $GLOBALS['config']['week_unit_price'],
            $GLOBALS['config']['mon_unit_price'],
            $GLOBALS['config']['tue_unit_price'],
            $GLOBALS['config']['wed_unit_price'],
            $GLOBALS['config']['thu_unit_price'],
            $GLOBALS['config']['fri_unit_price'],
            $GLOBALS['config']['sat_unit_price']
        );

    }

    /**
     * 更新数据
     *
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     *
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data, $where)
    {
        if (!empty($where)) $this->db->where($where, null, false);
        return $this->db->update($this->table, $data);
    }


    /**
     * 更新记录
     *
     * @param order_id $order_id 订单id
     * @param date     $date     日期
     * @param number   $number   人数
     *
     * @return boolean 返回数据 true/false
     */
    public function update_record($order_id = 0, $date = '', $number = 0)
    {
        if (!empty($order_id) && !empty($date) && !empty($number))
        {

            $row = $this->get_row(array('order_id' => $order_id, 'date' => "'".$date."'"));
            if ($row)
            {
                if ($row['number'] != $number)
                {
                    $this->edit_row(array('number' => $number), array('id' => $row['id']));
                }
            }
            else
            {
                $insert = array(
                    'sid' => $this->sid,
                    'order_id' => $order_id,
                    'number' => $number,
                    'date' => $date,
                    'add_time' => date('Y-m-d H:i:s'),
                    'action_user' => $this->session->userdata('admin_user_name')
                );
                $this->add_row($insert);
            }
        }
    }

    /**
     * 清理记录
     *
     * @param order_id $order_id 订单id
     * @param date     $date     日期
     * @param number   $number   人数
     *
     * @return boolean 返回数据 true/false
     */
    public function clear_record($order_id = 0, $start = '', $end = 0)
    {
        if (!empty($order_id) && !empty($start) && !empty($end))
        {

            $date_section = fomate_range_date($start, $end);



        }
    }




    /**
     * 一口价模式-计算天数
     *
     */
    public function calc_order_room_account($order_id = '',$start_date = '' ,$end_date = '',$order_number = 0)
    {
        if(!empty($order_id) && !empty($start_date) && !empty($end_date) && !empty($order_number))
        {
            $account = 0 ;
            //房间人数日志
            $numbers_log = $this->get_list(array('order_id' => $order_id));
            $numbers_array = array();
            foreach ($numbers_log as $key=>$item)
            {
                $numbers_array[$item['date']]= $item ;
            }

            $date_section = fomate_range_date($start_date, $end_date); // 日期区间

            foreach ($date_section as $index => $item)
            {
                $tmp_number = !empty($numbers_array[$item]) ? $numbers_array[$item]['number'] : $order_number;
                $account += $tmp_number * $this->data['book_unit_price'];
            }

            return  $account ;

        }
    }

    /**
     * 混合模式-计算价格(当天入住以新填写人数为准)
     *
     */
    public function calc_mix_room_account_exclude_today($order_id = '',$start_date = '' ,$end_date = '',$order_number = 0)
    {
        if(!empty($order_id) && !empty($start_date) && !empty($end_date) && !empty($order_number))
        {

            $account = 0 ;
            //房间人数日志
            $numbers_log = $this->get_list(array('order_id' => $order_id));
            $numbers_array = array();
            foreach ($numbers_log as $key=>$item)
            {
                $numbers_array[$item['date']]= $item ;
            }

            $date_section = fomate_range_date($start_date, $end_date); // 日期区间

            foreach ($date_section as $index => $item)
            {
                $tmp_number = (empty($numbers_array[$item]) || ($item == date('Y-m-d')) ) ? $order_number : $numbers_array[$item]['number'] ;
                $account += $tmp_number * $this->period_price_array[date('w',strtotime($item))];
            }

            return  $account ;

        }
    }


    /**
     * 一口价模式-计算价格(当天入住以新填写人数为准)
     *
     */
    public function calc_order_room_account_exclude_today($order_id = '',$start_date = '' ,$end_date = '',$order_number = 0)
    {
        if(!empty($order_id) && !empty($start_date) && !empty($end_date) && !empty($order_number))
        {

                $account = 0 ;
                //房间人数日志
                $numbers_log = $this->get_list(array('order_id' => $order_id));
                $numbers_array = array();
                foreach ($numbers_log as $key=>$item)
                {
                    $numbers_array[$item['date']]= $item ;
                }

                $date_section = fomate_range_date($start_date, $end_date); // 日期区间

                foreach ($date_section as $index => $item)
                {
                    $tmp_number = (empty($numbers_array[$item]) || ($item == date('Y-m-d')) ) ? $order_number : $numbers_array[$item]['number'] ;
                    $account += $tmp_number * $this->data['book_unit_price'];
                }

                return  $account ;

        }
    }


    /**
     * 独立价格模式-计算价格(当天入住以新填写人数为准)
     *
     */
    public function calc_independ_room_account_exclude_today($order_id = '',$start_date = '' ,$end_date = '',$order_number = 0)
    {
        if(!empty($order_id) && !empty($start_date) && !empty($end_date) && !empty($order_number))
        {



            $account = 0 ;
            //房间人数日志
            $numbers_log = $this->get_list(array('order_id' => $order_id));
            $numbers_array = array();
            foreach ($numbers_log as $key=>$item)
            {
                $numbers_array[$item['date']]= $item ;
            }

            $date_section = fomate_range_date($start_date, $end_date); // 日期区间

            foreach ($date_section as $index => $item)
            {
                $tmp_number = (empty($numbers_array[$item]) || ($item == date('Y-m-d')) ) ? $order_number : $numbers_array[$item]['number'] ;
                $account += $tmp_number * $this->data['book_unit_price'];
            }

            return  $account ;

        }
    }



    /**
     * 根据条件获取订单人数日志
     *
     * @param mixed  $where 查询条件
     * @param string $field 获取字段
     *
     * @return array $data 返回数据
     */
    public function get_list($where = '', $field = '')
    {
        if (!empty($where)) $this->db->where($where, null, false);
        if (!empty($field)) $this->db->select($field);
        $this->db->order_by('add_time', 'DESC');
        $query = $this->db->get($this->table);
        return $query->result_array();
    }

    /**
     * 添加数据
     *
     * @param array $data 数组
     *
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data = array())
    {
        if (!empty($data) && $this->db->insert($this->table, $data))
        {
            return $this->db->insert_id();
        }
        return false;
    }

    /**
     * 查询单条记录
     *
     * @param mixed  $where 查询条件 array|string
     * @param string $field 获取到的字段
     *
     * @return mixed 返回数据
     */
    public function get_row($where, $field = '*')
    {
        if (!empty($where)) $this->db->where($where, null, false);
        if (!empty($field)) $this->db->select($field);
        $query = $this->db->get($this->table, 1);
        return $query->row_array();
    }

    /**
     * 删除内容
     *
     * @param mixed $where 查询条件 array|string
     *
     * @return bool 返回数据
     */
    public function remove_row($where)
    {
        if (!empty($where)) return $this->db->delete($this->table, $where);
        return false;
    }


    public function get_order_id_by_log_id($log_id)
    {
        $log_id = intval($log_id);
        $sql = "select order_id from " . $this->table . " where log_id=$log_id ";
        $query = $this->db->query($sql);
        $res = $query->row_array();
        return $res['order_id'];
    }



    /**
     * 批量添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_batch($data){
        if(is_array($data)){
            if($this->db->insert_batch($this->table,$data)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }


}